package com.easygo.controller;

import com.easygo.api.ContentClient;
import com.easygo.api.ItemCatClient;
import com.xuexi.easygo.pojo.Content;
import com.xuexi.easygo.pojo.ItemCat;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/*
* 首页控制器
* */
@Controller
@Scope("prototype")
public class IndexController {

    @Resource
    ContentClient contentClient;

    @Resource
    ItemCatClient itemCatClient;

    //跳转到首页
    @RequestMapping({"/","index"})
    public String index(Model model){
        //加载分类
        getItemCats(model);
        //轮播图广告
        getContents(model);
        return "index";
    }


    public void getItemCats(Model model){
        //所有的项目
        List<ItemCat> itemCats = itemCatClient.getItemCats();
        //所有的一级分类
        List<ItemCat> itemCats_one = extractItemCats("1", itemCats);
        System.out.println("一级目录="+itemCats);
        //二级分类
        List<ItemCat> itemCats_two = extractItemCats("2", itemCats, itemCats_one);
        //三级目录
        List<ItemCat> itemCats_third = extractItemCats("3", itemCats, itemCats_two);
        //存值
        model.addAttribute("itemCats_one",itemCats_one);
        model.addAttribute("itemCats_two",itemCats_two);
        model.addAttribute("itemCats_third",itemCats_third);
    }

    public List<ItemCat> extractItemCats(String type,List<ItemCat>... sourceList){
        List<ItemCat> emptyList = new ArrayList<>();
        //所有的分类
        List<ItemCat> allList = sourceList[0];
        //所有的一级分类
        if("1".equals(type)){
            for(ItemCat itemCat:allList){
                if(itemCat.getParent_id().toString().equals("0")){
                    emptyList.add(itemCat);
                    if(emptyList.size()==10){
                        //前台只显示十个
                        break;
                    }
                }
            }
        }

        if("2".equals(type)){
            List<ItemCat> itemCats_one = sourceList[1];
            for (ItemCat itemCat : itemCats_one){
                for (ItemCat item : allList){
                    if(item.getParent_id().toString().equals(itemCat.getId().toString())){
                        emptyList.add(item);
                    }
                }
            }
        }
        if("3".equals(type)){
            List<ItemCat> itemCats_two = sourceList[1];
            for (ItemCat itemCat:itemCats_two){
                for(ItemCat item : allList){
                    if(item.getParent_id().toString().equals(itemCat.getId().toString())){
                        emptyList.add(item);
                    }
                }
            }
        }
        return emptyList;
    }




    //加载首页上需要的广告数据
    public void getContents(Model model){
        //1表示轮播图广告
        //不想写死，把广告列表id写到配置文件中
        List<Content> lunbo_contents = contentClient.getContentsByCategoryId(1);
        model.addAttribute("lunbo_contents",lunbo_contents);

        //生鲜广告
        List<Content> shengxian_contents = contentClient.getContentsByCategoryId(10);
        model.addAttribute("shengxian_contents",shengxian_contents);
    }

}
